package com.zkr.legalperson.service;


import com.zkr.legalperson.mapper.LagalPersonMapper;
import com.zkr.legalperson.model.*;
import com.zkr.planclass.enummodel.StatusEnum;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2020/12/16.
 */
@Service
public class LegalPersonServiceImpl implements LegalPersonService {


    @Autowired
    private LagalPersonMapper lagalPersonMapper;

    @Override
    public ResultVo query(LegalPersonSelectVo legalPersonSelectVo) {
        int count = lagalPersonMapper.queryCount(legalPersonSelectVo);
        List<LegalPlantClassVo> legalPlantClassVoList = lagalPersonMapper.query(legalPersonSelectVo);
        for (LegalPlantClassVo legalPlantClassVo : legalPlantClassVoList) {

            Integer enabled = legalPlantClassVo.getEnabled() == true ? 1 : 0;
            for (StatusEnum c : StatusEnum.values()) {
                if (c.getValue().equals(legalPlantClassVo.getEnabled())) {
                    legalPlantClassVo.setEnabledName(c.getName());
                }
            }

        }
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(legalPersonSelectVo.getPageIndex());
        allRecords.setDataList(legalPlantClassVoList);
        allRecords.setPageSize(legalPersonSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);


    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid LegalPersonUpdateStatusVo legalPersonUpdateStatusVo) {

        LegalPlantClassVo legalPlantClassVo = new LegalPlantClassVo();
        BeanUtils.copyProperties(legalPersonUpdateStatusVo, legalPlantClassVo);
        legalPlantClassVo.setUpdateTime(new Date());
        legalPlantClassVo.setUpdateName("李四");
        lagalPersonMapper.updateStatus(legalPlantClassVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo add(@Valid LegalPersonAddVo legalPersonAddVo) {


        //验证是否已经存在编码或名称
        LegalPlantClassVo legalPlantClassCode = lagalPersonMapper.queryByCode(legalPersonAddVo.getLegalPersonCode());
        if (legalPlantClassCode != null) {
            return ResultBuilderVo.error("工厂编码已经存在");
        }
        LegalPlantClassVo legalPlantClassVo = new LegalPlantClassVo();
        legalPlantClassVo.setCreateName("李四");
        legalPlantClassVo.setCreateTime(new Date());
        legalPlantClassVo.setUpdateName("李四");
        legalPlantClassVo.setUpdateTime(new Date());

        //把左边的数据赋值到右边
        BeanUtils.copyProperties(legalPersonAddVo, legalPlantClassVo);
        //入库
        lagalPersonMapper.insertPlantClass(legalPlantClassVo);
        return ResultBuilderVo.success();

    }

    @Override
    public ResultVo queryLegalPersonById(Integer userid) {
        List<LegalPersonVo> legalPersonVoList = lagalPersonMapper.queryLegalPersonById(userid);

        return ResultBuilderVo.success(legalPersonVoList);

    }

    @Override
    @Transactional
    public ResultVo update(@Valid LegalPersonUpdateVo legalPersonUpdateVo) {
        //2.只有禁用状态才能修改描述
        //3.修改数据库
        LegalPlantClassVo legalPlantClassVo = lagalPersonMapper.queryById(legalPersonUpdateVo.getLegalPlantCode());
        if (legalPlantClassVo.getEnabled() == true) {
            return ResultBuilderVo.error("修改时必须为禁用状态");
        }
        LegalPlantClassVo legalPlantClassVo1 = new LegalPlantClassVo();
        BeanUtils.copyProperties(legalPersonUpdateVo, legalPlantClassVo1);
        legalPlantClassVo1.setUpdateTime(new Date());
        legalPlantClassVo1.setUpdateName("李四");
        lagalPersonMapper.update(legalPlantClassVo1);
        return ResultBuilderVo.success();

    }
}

